'use strict';

App.controller('publishMakeBillDetailController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
	
	if($state.params.model== null || $state.params.model == ''){
		 $state.go('publish', null);
		 return;
	}
	  
	$scope.publishMakeBillDetailQuery = {};
	$scope.customerId =	$rootScope.user.sourceId;// 根据登录用户获得客户id
	
    $scope.publishMakeBillBill = $state.params.model;
    $scope.publishBillStatus = $state.params.model.publishBillStatus;
	
	$scope.publishMakeBillDetailTable = {
			query : function() {
				var param = {};
				param.customerId = $scope.customerId;
				param.billNo = $scope.publishMakeBillDetailQuery.billNo;
				param.billId = $scope.publishMakeBillBill.billId;
				param.pageIndex = 0;
				param.pageSize = 10;
				Page.ajaxPost('www/publishBill/queryMakeBillInfo.do', param, function (resp) {
					 $scope.dataArray = resp.data.dataArray;
					 
					/*// 调用分页
				    	$scope.totalPages = resp.data.totalPages;
						laypage({
							cont : 'page_bar',
							pages : $scope.totalPages,// 得到总页数
							skin : '#12bdce',
							skip : true, // 是否开启跳页
							prev : '<', // 若不显示，设置false即可
							next : '>', // 若不显示，设置false即可
							groups : 5, //连续分页数
							curr: 1, //初始化当前页
							jump : function(obj,first) {// 触发分页后的回调
								obj是一个object类型。包括了分页的所有配置信息。
								 first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。
								if(!first){
									param.pageIndex = obj.curr - 1;
									Page.ajaxPost('www/publishBill/queryMakeBillDetails.do', param, function(resp) {
										$scope.dataArray = resp.data.dataArray;
										$scope.totalPages = resp.data.totalPages;
									});
								}
							}
						})*/
				 });
			},
			complete : function(data) {
	            $scope.completeBill = data;
	            $scope.completeModal.show();
	        },
	        view : function(data) {
	            $scope.completeBill = data;
	            $scope.completeViewModal.show();
	        },
	        callBack : function() {
		    	 window.history.back();// 需要拿到上刊单id
		    },
		    finishAll : function() {
		    	// 直接改变上刊单的状态，改为“已完工”
		    	Page.confirm("确定将该上刊单完工吗？", function () {
		    	 Page.ajaxPostQuiet('www/publishBill/finishAll.do', {billId : $scope.publishMakeBillBill.billId}, function (resp) {
		    		 $state.go('publish', null);
		            });
		    	  });
		    }
	}
	
	 $scope.completeModal = {
	        onOkClick : function() {
	        	if($scope.completeBill.completeTime==''||$scope.completeBill.completeTime==undefined){
	        		Page.notice("完工时间不能为空!")
	        		return ;
	        	}
	            Page.ajaxPostQuiet('www/publishBill/completeBill.do', $scope.completeBill, function (resp) {
	            	$scope.publishMakeBillDetailTable.query();
	                $scope.completeModal.hide();
	            });
	        },
	        onHidden : function() {

	        }
	    }
	
	$scope.publishMakeBillDetailTable.query();
	
    $scope.fileModal = {// 完工照仅支持单文件上传
    		deleteFile : function(file){
    			/*var files =$scope.completeBill.img;
    			 for (var i = 0; i < files.length; i++) {
    				 if(files[i].path.indexOf(file.path)>-1){
    						 $scope.completeBill.img.splice(i,1);
    				 }
    			 }*/
    			$scope.completeBill.img = null;
    		},
		  downloadFile : function(filePath) {
	        	document.location.href = "publishBill/downLoadFile.do?publishFile="+filePath;
	        },
    }
	
}]);